/*
 * DoctorOrder.java
 * 
 * @version 1.0
 * CS 2340 Team 10: Cyberdyne Systems
 */

package edu.gatech.oad.cyberdoc.domain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * Stores the information about an DoctorOrder
 * 
 * @author Miles Gantt
 * @version 1.0
 */
public class DoctorOrder {
	/**
	 * The logger for this class.
	 */
	private static final Logger LOG;

	/**
	 * A map storing all instances of DoctorOrder for quick look-up.
	 */
	private static final Map<Integer, DoctorOrder> ORDERS;

	/**
	 * The DoctorOrder ID number.
	 */
	private int id;

	/**
	 * The patient associated with this DoctorOrder.
	 */
	private Patient patient;

	/**
	 * The doctor associated with the DoctorOrder.
	 * 
	 * TODO Abstract to multiple doctors/staff members
	 */
	private Doctor doctor;

	/**
	 * The date the services in this DoctorOrder were performed.
	 */
	private TreatmentRecord tr;

	/**
	 * The line items in this DoctorOrder.
	 */
	private List<DoctorOrderInfo> items;

	static {
		ORDERS = new HashMap<Integer, DoctorOrder>();
		LOG = Logger.getLogger("edu.gatech.oad.cyberdoc.domain.DoctorOrder");
	}

	/**
	 * Creates a new DoctorOrder with all of the possible information supplied.
	 * 
	 * @param id
	 *            The DoctorOrder ID number
	 * @param patient
	 *            The patient associated with this DoctorOrder
	 * @param tr
	 *            The treatment record that the Doctor Order is attached too
	 * @param doctor
	 *            The doctor associated with this DoctorOrder
	 * @param items
	 *            The line items in this DoctorOrder
	 */
	public DoctorOrder(int id, Patient patient, Doctor doctor,
			TreatmentRecord tr, List<DoctorOrderInfo> items) {
		this.id = id;
		this.patient = patient;
		this.doctor = doctor;
		this.tr = tr;
		this.items = items;

		ORDERS.put(id, this);
		LOG.info("DoctorOrder \"" + id + "\" created for \"" + patient + "\".");
	}

	/**
	 * Creates a new DoctorOrder with no line items.
	 * 
	 * @param id
	 *            The DoctorOrder ID number
	 * @param patient
	 *            The patient associated with this DoctorOrder
	 * @param tr
	 *            The treatment record that the Doctor Order is attached too
	 * @param doctor
	 *            The doctor associated with this DoctorOrder
	 */
	public DoctorOrder(int id, Patient patient, Doctor doctor,
			TreatmentRecord tr) {
		this(id, patient, doctor, tr, new ArrayList<DoctorOrderInfo>());
	}

	/**
	 * Adds a line item to this DoctorOrder.
	 * 
	 * @param item
	 *            The line item to add
	 */
	public void addItem(DoctorOrderInfo item) {
		items.add(item);

		LOG.info("DoctorOrder item \"" + item + "\" added to DoctorOrder \""
				+ id + "\"");
	}

	/**
	 * Retrieves a previously created DoctorOrder given the DoctorOrder's ID
	 * number.
	 * 
	 * @param id
	 *            The DoctorOrder's ID number
	 * 
	 * @return The DoctorOrder associated with the ID number
	 */
	public static DoctorOrder getDoctorOrder(Integer id) {
		return ORDERS.get(id);
	}

	/**
	 * Returns a string representation of this DoctorOrder.
	 * 
	 * @return This DoctorOrder as a String
	 */
	@Override
	public String toString() {
		final StringBuffer buffer = new StringBuffer();

		buffer.append("DoctorOrder ID: ");
		buffer.append(id);
		buffer.append("\nPatient: ");
		buffer.append(patient);
		buffer.append("\nDoctor: ");
		buffer.append(doctor);
		buffer.append("\nItems:");

		for (DoctorOrderInfo item : items) {
			buffer.append("\n\t");
			buffer.append(item);
		}

		return buffer.toString();
	}

	/**
	 * @param tr the tr to set
	 */
	public void setTr(TreatmentRecord tr) {
		this.tr = tr;
	}

	/**
	 * @return the tr
	 */
	public TreatmentRecord getTr() {
		return tr;
	}
}
